package com.css.fxfzaqbz.modules.data.repository.impl;


import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.queryParams.SeismicHazardAssessQueryParams;
import com.css.fxfzaqbz.modules.data.repository.AssistOpinionAnalyseRepository;
import com.css.fxfzaqbz.modules.data.repository.SeismicHazardAssessRepository;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class AssistOpinionAnalyseRepositoryImpl implements AssistOpinionAnalyseRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Override
    public Map<String,Object> getAllAssistOpinionAnalyseParams(SeismicHazardAssessQueryParams queryParams) {
        StringBuilder sql = new StringBuilder(" " +
                "select ia.*,ee.ee_status ,bd.bd_status\n" +
                "from sys_important_activity ia \n" +
                "left join sys_earth_assess ee  \n" +
                "on ia.ia_id = ee.ee_activeid\n" +
                "left join sys_big_disaster bd  \n" +
                "on ia.ia_id =  bd.bd_status ");

        StringBuilder orderSql = new StringBuilder(" order by ia.ia_number");

        if(PlatformObjectUtils.isEmpty(queryParams.getIaActivityStatus())){
            queryParams.setIaActivityStatus(YNEnum.N.toString());
        }
        StringBuilder whereSql = new StringBuilder(" where ia.del_flag = '" + YNEnum.N.toString() + "' and ia.ia_asses = '"+YNEnum.Y+"' and ia_activity_status='"+queryParams.getIaActivityStatus()+"' ");


        if (!PlatformObjectUtils.isEmpty(queryParams.getIaNumber())) {
            whereSql.append(" and ia.ia_number ='" + queryParams.getIaNumber() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getIaName())) {
            whereSql.append(" and ia.ia_name ='" + queryParams.getIaName() + "'");
        }

        int firstIndex=(queryParams.getCurPage()-1) * queryParams.getPageSize();
        int lastIndex=queryParams.getCurPage() * queryParams.getPageSize();
       // sql.append(whereSql).append(orderSql).toString();
        sql.append(whereSql).toString();
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(SeismicHazardAssessQueryParams.class)).size();
        List<SeismicHazardAssessQueryParams> seismicHazardAssessQueryParams = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(SeismicHazardAssessQueryParams.class));
        Map<String,Object> map=new HashMap();
        map.put("total",total);
        map.put("rows",seismicHazardAssessQueryParams);
        return map;
    }

}
